package com.eoner.managerlibrary.sobot;

import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;

import com.eoner.baselibrary.utils.TimeUtil;
import com.eoner.common.config.Config;
import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.managerlibrary.R;
import com.eoner.managerlibrary.user.UserDataManager;
import com.sobot.chat.SobotApi;
import com.sobot.chat.api.apiUtils.SobotBaseUrl;
import com.sobot.chat.api.model.Information;
import com.sobot.chat.api.model.OrderCardContentModel;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
* 智齿管理类
* */
public class SobotManager {

    //智齿客服
    private final static String SOBOT_APP_KEY = "315c70dd6a57492f9f892eb9281c4d87";
    private final static String PLATFORM_UNION_CODE = "1074";
    private final static String PLATFORM_SECRET_KEY = "Ym7n726q7aO5";

    public static void initSobot(Context context, ContentResolver contentResolver) {
        //智齿客服的初始化
        String ANDROID_ID = Settings.System.getString(contentResolver, Settings.System.ANDROID_ID);
        SobotBaseUrl.setHost("https://www.soboten.com");
        SobotApi.initSobotSDK(context, SOBOT_APP_KEY, ANDROID_ID);
        SobotApi.initPlatformUnion(context, PLATFORM_UNION_CODE, PLATFORM_SECRET_KEY);
    }

    public static Information getBaseInfo(){
        Information info = new Information();
        info.setAppkey(SOBOT_APP_KEY);
        info.setShowSatisfaction(true);
        info.setTitleImgId(R.drawable.sobot_title_bkg);
        if(UserDataManager.getInstance().getUserCacheData() != null){
            info.setUid(TextUtils.isEmpty(UserDataManager.getInstance().getCustomerId())? SPUtils.getInstance().getString(Config.CUSTOMERID):UserDataManager.getInstance().getCustomerId());
            info.setUname(UserDataManager.getInstance().getNickName());
            info.setTel(UserDataManager.getInstance().getMobile());
        }
        return info;
    }

    public static void setSobotFlow(Context mContext, String flowCompanyId, String flowGroupId, String flowType){
        //设置溢出商城主站公司id
        SobotApi.setFlowCompanyId(mContext,flowCompanyId);
        //设置溢出商城主站公司技能组Id
        SobotApi.setFlowGroupId(mContext,flowGroupId);
        //设置是否溢出到主商户flowType  0-不溢出 , 1-全部溢出，2-忙碌时溢出，3-不在线时溢出,默认不溢出
        SobotApi.setFlowType(mContext,flowType);
    }

    public static void setOrderCardInfo(Information info,String orderNo,String orderStatus,int totalFee,String goodsCount,String createTime,String productName,String productImageUrl){

        List<OrderCardContentModel.Goods> goodsList = new ArrayList<>();
        goodsList.add(new OrderCardContentModel.Goods(productName, productImageUrl));
        OrderCardContentModel orderCardContent = new OrderCardContentModel();
        //订单编号（必填）
        orderCardContent.setOrderCode(orderNo);
        //订单状态
        orderCardContent.setOrderStatus(get_sobot_order_status(orderStatus));
        //订单总金额
        orderCardContent.setTotalFee(totalFee);
        //订单商品总数
        orderCardContent.setGoodsCount(goodsCount);
        //订单链接
        //orderCardContent.setOrderUrl("https://item.jd.com/1765513297.html");
        //订单创建时间
        try {
            orderCardContent.setCreateTime("" + TimeUtil.strToLong(createTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //转人工后是否自动发送
        orderCardContent.setAutoSend(true);
        //订单商品集合
        orderCardContent.setGoods(goodsList);
        //订单卡片内容
        info.setOrderGoodsInfo(orderCardContent);
        SobotApi.setHyperlinkListener(new SobotHyperlinkListener());

    }


    private static int get_sobot_order_status(String orderStatus) {
        // 智齿 待付款: 1   待发货: 2   运输中: 3   派送中: 4   已完成: 5   待评价: 6   已取消: 7
        // 中华大师汇 0：待付款 1： 已付款/待发货 2：待收货 3：订单关闭 4：已收货/订单成功 8：订单完成 9：退款完成
        int status = 4;
        if("0".equals(orderStatus)){
            return 1;
        }
        if("1".equals(orderStatus)){
            return 2;
        }
        if("2".equals(orderStatus)){
            return 3;
        }
        if("3".equals(orderStatus) | "8".equals(orderStatus) | "9".equals(orderStatus)){
            return 4;
        }
        if("4".equals(orderStatus)){
            return 4;
        }
        return status;
    }



}
